/*
 * tss_ptr.h
 *
 *  Created on: 2008-9-20
 *      Author: ant
 */

#ifndef TSS_PTR_H_
#define TSS_PTR_H_

#include "tss.h"

BEGIN_NAMESPACE_TOXIC

template <typename T>
class tss_ptr {
public:
	tss_ptr(cleanup_route cleanup = default_cleanup)
	:_tss(cleanup) {
	}

	~tss_ptr() {}

	T* get() const {
		return static_cast<T*>(_tss.get());
	}

	T* safe_get() {
		T* value = get();
		if (value == 0) {
			value = reset(new T());
		}
		return value;
	}

	T* reset(T* value = 0) {
		if (value != get()) {
			_tss.set(value);
		}
		return value;
	}

	T* release() {
		T* value = get();
		reset();
		return value;
	}

	T& operator *() const {
		return *get();
	}

	T* operator ->() const {
		return get();
	}

private:
	static void default_cleanup(void* value) {
		delete static_cast<T*>(value);
	}

private:
	detail::tss _tss;
};

END_NAMESPACE_TOXIC

#endif /* TSS_PTR_H_ */
